/**
 * create by 9-3-17
 * 
 */

const orderModalController = require('./order.modal.controller')
orderController.$inject = ['$scope', '$location', 'orderService', '$uibModal'];

function orderController($scope, $location, orderService, $modal) {
    var od = this;
    var orders = [];
    // od.findOrderByCustomerId = function(){
    let user = JSON.parse(sessionStorage.getItem('customer'));
    /**
     * for test
     */
    let temp = sessionStorage.getItem("customer");
    console.log(temp);
    let customerId = JSON.parse(sessionStorage.getItem("customer")).id;


    // let customerId = user.id;




    console.log('customerId...' + customerId);
    if (customerId != null) {
        orderService.findOrderInfoByCustomerId(customerId, function (err, result) {
            if (err) {
                console.log(err);
            } else {
                console.log(result);
                $scope.orders = result;
            }
        });
    }

    od.confirmOrder = function (order) {
        // var r = confirm("是否确认收货？");
        // if (r == true) {
            order.status = "订单完成";
            orderService.updateOrderStatus(order, function (err, result) {
                if (err) {
                    console.log("error " + err.message);
                }
                else {
                    console.log(result);
                    if (result.nModified == 1 && result.ok == 1) {
                        console.log("success to call back");
                        // alert("该订单已确认完成");
                    }
                }
            });
        // } else {

        //     return false;
        // }





    }


    od.openModal = function (order) {
        var modalInstance = $modal.open({
            template: require('../layouts/order.modal.html'),
            controller: orderModalController,
            resolve: {
                order: function () {
                    return order;
                },
                confirmOrder : function(){
                    return  od.confirmOrder;
                }

            }
        });
        modalInstance.opened.then(function () {//when the modal open
            console.log('modal is opened');
        });
        modalInstance.result.then(function (result) {
            console.log(result);
        }, function (reason) {
            console.log(reason);
        });
    };



}

module.exports = orderController;